package com.ellricky.baselib.mvp;

import com.andr.common.http.callback.JOCallback;
import com.andr.common.ui.mvp.BasePresenter;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.http.HttpUtils;
import com.ellricky.baselib.http.callback.ECallback;
import com.ellricky.baselib.util.EAppUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class BaseDetailPresenter extends BasePresenter<BaseDetailView> {

    private BaseDetailView view;

    public BaseDetailPresenter(BaseDetailView detailView) {
        this.view = detailView;
    }


    public <T> void loadDetail(String path, String id, ECallback<T> callback) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        loadDetail(path, paramMap, callback);
    }

    public <T> void loadDetail(String path, Map<String, Object> paramMap, ECallback<T> callback) {
        String url = EAppUtil.getApiHost() + path;
        if (paramMap == null) paramMap = new HashMap<>();
        HttpUtils.getInstance().post(url, paramMap, callback);
    }

    public void sendBackRequest(String path, String id) {
        sendBackRequest(path, id, BaseConstant.RESULT_CODE_DETAIL);
    }

    public void sendBackRequest(String path, String id, final int resultCode) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        sendBackRequest(path, paramMap, resultCode);
    }

    public void sendBackRequest(String path, Map<String, Object> paramMap) {
        sendBackRequest(path, paramMap, BaseConstant.RESULT_CODE_DETAIL);
    }

    public void sendBackRequest(String path, Map<String, Object> paramMap, final int resultCode) {
        String url = EAppUtil.getApiHost() + path;
        if (paramMap == null) paramMap = new HashMap<>();

        HttpUtils.getInstance().post(url, paramMap, new JOCallback(view.getAct()) {
            @Override
            public void onSuccess(JSONObject dataJO, String msg) {
                view.onBackPage(resultCode);
            }
        });
    }


}
